import Vue from 'vue'
import axios from 'axios'

import Router from 'vue-router'
import layout from '../views/layout/layout'

import login from '../views/entrance/login'
import reg from '../views/entrance/reg'
import forgetPassword from '../views/entrance/forgetPassword'
import user from '../views/user/user.vue'
import notice from '../views/notice/notice.vue'
import noticeDetail from '../views/notice/noticeDetail.vue'

import home from '../views/home/home'
import routerTransit from '../views/routerTransit/routerTransit'

import shopManage from '../views/shopManage/shopManage.vue'
import goodsManage from '../views/shopManage/goodsManage.vue'

import createDragPlan from '../views/plan/createDragPlan.vue'
import createPlan from '../views/plan/createPlan.vue'
import planList from '../views/plan/planList.vue'
import planDetail from '../views/plan/planDetail.vue'

import salesWaitingOrder from '../views/salesManage/salesWaitingOrder.vue'
import salesWaitReceiveOrder from '../views/salesManage/salesWaitReceiveOrder.vue'
import salesBeingExecutedOrder from '../views/salesManage/salesBeingExecutedOrder.vue'
import salesWaitCheckOrder from '../views/salesManage/salesWaitCheckOrder.vue'
import salesSuccessOrder from '../views/salesManage/salesSuccessOrder.vue'
import salesFailOrder from '../views/salesManage/salesFailOrder.vue'
import salesAllOrder from '../views/salesManage/salesAllOrder.vue'

import inviteAssessOrder from '../views/assessManage/inviteAssessOrder.vue'
import addToAssessOrder from '../views/assessManage/addToAssessOrder.vue'
import appealAssessOrder from '../views/assessManage/appealAssessOrder.vue'

import flowWaitingOrder from '../views/flowManage/flowWaitingOrder.vue'
import flowWaitReceiveOrder from '../views/flowManage/flowWaitReceiveOrder.vue'
import flowBeingExecutedOrder from '../views/flowManage/flowBeingExecutedOrder.vue'
import flowWaitCheckOrder from '../views/flowManage/flowWaitCheckOrder.vue'
import flowSuccessOrder from '../views/flowManage/flowSuccessOrder.vue'
import flowFailOrder from '../views/flowManage/flowFailOrder.vue'
import flowAllOrder from '../views/flowManage/flowAllOrder.vue'

import appealWorkOrder from '../views/appealManage/appealWorkOrder.vue'

import reCharge from '../views/financeManage/reCharge.vue'
import withdraw from '../views/financeManage/withdraw.vue'
import fundingDetails from '../views/financeManage/fundingDetails.vue'
import charges from '../views/financeManage/charges.vue'
import merchantRebate from '../views/financeManage/merchantRebate.vue'
import transferProblem from '../views/financeManage/transferProblem.vue'
import planBill from '../views/financeManage/planBill.vue'
import popNotice from '../views/home/popnotice.vue'
import toolManage from "@/views/toolManage/toolManage";
import lipingManage from "@/views/toolManage/lipingManage";
Vue.use(Router)
const router = new Router({
  mode: 'hash',
  routes: [{
    path: '/',
    redirect: 'login',
	  noMenu: true
  },{
    path: '/login',
    name: '登录',
    component: login,
    noMenu: true
  },{
    path: '/reg',
    name: '注册',
    component: reg,
    noMenu: true
  },{
    path: '/forgetPassword',
    name: '忘记密码',
    component: forgetPassword,
    noMenu: true
  },{
    path: '/popNotice',
    name: '公告弹窗',
    component: popNotice,
    noMenu: true
  },{
    // 首页
    path: '/home1',
    component: layout,
    noMenu: false,
    children: [{
      path: '/home',
      name: '控制台',
      component: home,
      meta: {
        requiresAuth: true,
        noChildren: true,
        menuIcon: 'el-icon-ali-control'
      }
    }, {
      path: '/toolManage',
      name: '礼品网/电商工具箱',
      component: routerTransit,
      meta: {
        requiresAuth: true,
        noChildren: false,
        menuIcon: 'el-icon-ali-shop'
      },
      children: [{
        name: '电商工具箱',       // todo 工具箱
        isShow: true,
        path: '/toolManage/toolManage',
        component: toolManage,
      },{
        name: '礼品网一键发货',
        isShow: true,
        path: '/toolManage/lipingManage',
        component: lipingManage,
      }]
    },{
      path: '/shopManage',
      name: '店铺管理',
      component: routerTransit,
      meta: {
        requiresAuth: true,
        noChildren: false,
        menuIcon: 'el-icon-ali-shop'
      },
      children: [{
        path: '/shopManage/shopManage',
        name: '店铺管理',
        isShow: true,
        component: shopManage
      },{
        path: '/shopManage/goodsManage',
        name: '商品管理',
        isShow: true,
        component: goodsManage
      },{
        path: '/user',
        name: '个人中心',
        component: user,
        isShow: false,
      },{
        path: '/notice',
        name: '公告列表',
        component: notice,
        isShow: false,
      },{
        path: '/noticeDetail',
        name: '公告列表',
        component: noticeDetail,
        isShow: false,
      }]
    },{
      path: '/plan',
      name: '新建计划',
      component: routerTransit,
      meta: {
        requiresAuth: true,
        noChildren: false,
        menuIcon: 'el-icon-ali-create-plan'
      },
      children: [{
        path: '/plan/createSalsePlan',
        name: '新建销量计划',
        isShow: true,
        component: createPlan
      },{
        path: '/plan/createFlowPlan',
        name: '新建流量计划',
        isShow: true,
        component: createPlan
      },{
        path: '/plan/salesPlanList',
        name: '销量计划列表',
        isShow: true,
        component: planList
      },{
        path: '/plan/flowPlanList',
        name: '流量计划列表',
        isShow: true,
        component: planList
      },{
        path: '/plan/createDragPlan',
        name: '新建淘宝一拖N计划',
        isShow: true,
        component: createDragPlan
      },{
        path: '/plan/salesPlanDetail',
        name: '销量计划详情',
        isShow: false,
        component: planDetail
      },{
        path: '/plan/flowPlanDetail',
        name: '流量计划详情',
        isShow: false,
        component: planDetail
      }]

    },{
      path: '/salesManage',
      name: '销量管理',
      number: 0,
      component: routerTransit,
      meta: {
        requiresAuth: true,
        noChildren: false,
        menuIcon: 'el-icon-ali-sales'
      },
      children: [{
        path: '/salesManage/waitingOrder',
        name: '等待执行',
        isShow: true,
        component: salesWaitingOrder
      },{
        path: '/salesManage/waitReceiveOrder',
        name: '已派单未领取',
        isShow: true,
        component: salesWaitReceiveOrder
      },{
        path: '/salesManage/beingExecutedOrder',
        name: '正在执行',
        isShow: true,
        component: salesBeingExecutedOrder
      },{
        path: '/salesManage/waitCheckOrder',
        name: '待审核订单',
        isShow: true,
        number: 0,
        component: salesWaitCheckOrder
      },{
        path: '/salesManage/successOrder',
        name: '已完成订单',
        isShow: true,
        component: salesSuccessOrder
      },{
        path: '/salesManage/failOrder',
        name: '失败订单',
        isShow: true,
        component: salesFailOrder
      },{
        path: '/salesManage/allOrder',
        name: '全部订单',
        isShow: true,
        component: salesAllOrder
      }]
    }, {
      path: '/assessManage',
      name: '评价管理',
      number: 0,
      component: routerTransit,
      meta: {
        requiresAuth: true,
        noChildren: false,
        menuIcon: 'el-icon-ali-assess'
      },
      children: [{
        path: '/assessManage/inviteAssessOrder',
        name: '邀请评价',
        isShow: true,
        component: inviteAssessOrder
      },{
        path: '/assessManage/addToAssessOrder',
        name: '评价订单',
        isShow: true,
        number: 0,
        component: addToAssessOrder
      },{
        path: '/assessManage/appealAssessOrder',
        name: '评价申诉',
        isShow: true,
        number: 0,
        component: appealAssessOrder
      }]
    },{
      path: '/flowManage',
      name: '流量管理',
      number: 0,
      component: routerTransit,
      meta: {
        requiresAuth: true,
        noChildren: false,
        menuIcon: 'el-icon-ali-flow'
      },
      children: [{
        path: '/flowManage/waitingOrder',
        name: '等待执行',
        isShow: true,
        component: flowWaitingOrder
      },{
        path: '/flowManage/waitReceiveOrder',
        name: '已派单未领取',
        isShow: true,
        component: flowWaitReceiveOrder
      },{
        path: '/flowManage/beingExecutedOrder',
        name: '正在执行',
        isShow: true,
        component: flowBeingExecutedOrder
      },{
        path: '/flowManage/waitCheckOrder',
        name: '待审核订单',
        isShow: true,
        number: 0,
        component: flowWaitCheckOrder
      },{
        path: '/flowManage/successOrder',
        name: '已完成订单',
        isShow: true,
        component: flowSuccessOrder
      },{
        path: '/flowManage/failOrder',
        name: '失败订单',
        isShow: true,
        component: flowFailOrder
      },{
        path: '/flowManage/allOrder',
        name: '全部订单',
        isShow: true,
        component: flowAllOrder
      }]
    },{
      path: '/appealManage',
      name: '申诉管理',
      component: routerTransit,
      meta: {
        requiresAuth: true,
        noChildren: false,
        menuIcon: 'el-icon-ali-appeal'
      },
      children: [{
        path: '/appealManage/appealWorkOrder',
        name: '申诉工单',
        isShow: true,
        component: appealWorkOrder
      }]
    },{
      path: '/financeManage',
      name: '财务管理',
      component: routerTransit,
      meta: {
        requiresAuth: true,
        noChildren: false,
        menuIcon: 'el-icon-ali-finance'
      },
      children: [{
        path: '/financeManage/reCharge',
        name: '充值中心',
        isShow: true,
        component: reCharge
      },{
        path: '/financeManage/withdraw',
        name: '提现中心',
        isShow: true,
        component: withdraw
      },{
        path: '/financeManage/fundingDetails',
        name: '资金明细',
        isShow: true,
        component: fundingDetails
      },{
        path: '/financeManage/charges',
        name: '收费标准',
        isShow: true,
        component: charges
      },{
        path: '/financeManage/merchantRebate',
        name: '人工返款订单',
        isShow: true,
        component: merchantRebate
      },
      // {
      //   path: '/financeManage/planBill',
      //   name: '计划账单',
      //   isShow: true,
      //   component: planBill
      // },
      {
        path: '/financeManage/transferProblem',
        name: '转账帮助',
        isShow: false,
        component: transferProblem
      }]
    }]
  }]
})
router.beforeEach((to, from, next) => {
    if(to.query && to.query.token){
      window.localStorage.setItem('token',to.query.token)
      next('/home')
    } else {
      if (to.path === '/login' || to.path === '/reg' || to.path === '/forgetPassword') {
         next()
      } else {
        let token = window.localStorage.getItem('token') ? window.localStorage.getItem('token') : ''

        axios.post(
          "baseapi/token/verifyToken?token="+ token
        ).then((res) => {
          if(res.data.code == 1){
            next()
          }else {
            next('/login')
          }
        }).catch((error) => {
          console.log(error)
        })

      }
    }
  })

  export default router
